<?php 
/*
 module:		登录
 create_time:	2024-04-24 16:47:04
 author:
 contact:
*/

namespace app\api\controller;

use app\api\service\LoginService;
use app\api\model\Login as LoginModel;
use lib\Dysms;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;
use utils\weixinpay\wechatpay;

class Login extends Common {

    /**start**/
	/**
	* @api {post} /Login/wxLogin 01、登录
	* @apiGroup Login
	* @apiVersion 1.0.0
	* @apiDescription  账号密码登录

	* @apiParam (输入参数：) {string}     		code 登录用户名
	* @apiParam (输入参数：) {string}     		 登录密码

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.msg 返回成功消息
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","msg":"操作成功"}
	* @apiErrorExample {json} 02 失败示例
	* {"status":"201","msg":"操作失败"}
	*/
	function wxLogin(){
		$postField = 'code,second';
		$data = $this->request->only(explode(',',$postField),'post',null);
        //有token返回uid
        $uid = $this->getUidByToken($this->request);

        if($uid != ""){
            $res = Db::name("user")->where("uid",$uid)->find();
            if(empty($res)){
                throw new ValidateException('请授权1');
            }
        }else{
            $code = $data["code"];
            if(empty($code)){
                throw new ValidateException('请授权2');
            }
            $wechat = new wechatpay();
            $user_openid = $wechat->GetOpenid($data);
            if(!empty($user_openid) &&  !isset($user_openid["openid"])){
                throw new ValidateException('请授权3');
            }else{
                $res = LoginService::wxLogin($data,$user_openid);
            }
        }
        //授权的账号
		return $this->ajaxReturn($this->successCode,'登陆成功',$res,$this->setToken($res['uid']));
	}

    /**
     *
     * 手机号登录
     * @return false|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function phoneLogin(){
        $postField = 'phone,code,uuid';
        $data = $this->request->only(explode(',',$postField),'post',null);

        if(empty($data['phone'])){
            throw new ValidateException('请输入手机号');
        }
        if(empty($data['code'])){
            throw new ValidateException('请输入验证码');
        }
        if(empty($data['uuid'])){
            throw new ValidateException('请获取验证码');
        }
        //有token返回uid
        $uid = $this->getUidByToken($this->request);
        //没有uid未登录
        if(empty($uid)){

            $res= LoginService::phoneLogin($data);

        }else{
            $res = LoginModel::where("uid",$uid)->find();
        }
        //授权的账号
        return $this->ajaxReturn($this->successCode,'登陆成功',$res,$this->setToken($res['uid']));
    }

    /**
     * 发送验证码
     * @return void
     */
    public function sendCode(){
        $postField = 'phone';
        $data = $this->request->only(explode(',',$postField),'post',null);
        $phone = $data["phone"];

        $pattern = '/^1[3456789]\d{9}$/';
        if (!preg_match($pattern, $phone)) {
            throw new ValidateException("请输入正确的手机号");
        }


        $uuid = doOrderSn("dx");
        $code = rand(1000,9999);
        $sms = new Dysms();
        $result = $sms->send($phone, $code);
        if($result["errcode"] == 0 && $result["errmsg"] == "success"){
            cache($uuid,$code,600);
            $res["uuid"] = $uuid;
            return $this->ajaxReturn($this->successCode,'发送成功',$res);
        }else{
            throw new ValidateException("发送失败，稍后再试");
        }

    }


    /**end**/
}

